package com.rfid.emergencywarehouse.presenter;

import com.rfid.emergencywarehouse.net.NetApi;
import com.rfid.emergencywarehouse.net.entity.ShelfingRes;
import com.rfid.emergencywarehouse.view.IShelfingView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ShelfingPresenter<T extends IShelfingView> extends ReadTagPresenter<T>{

    public void shelfing(String location, List<String> barcodes) {
        mView.onShowLoading("正在上架");
        mExcutor.execute(new Runnable() {
            @Override
            public void run() {
                boolean s = false;
                String msg = "";
                try {
                    ShelfingRes res = NetApi.INSTANCE.shelfing(location,barcodes);
                    if (res.getCode() == 0) {
                        s = true;
                    } else {
                        s = false;
                        msg = res.getMsg();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    msg = e.getMessage();
                }
                String finalMsg = msg;
                boolean finalS = s;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mView != null) {
                            mView.onHideLoading();
                            mView.onShelfingResult(finalS, finalMsg);
                        }
                    }
                });
            }
        });
    }

    @Override
    protected void onShelf(String shelf) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mView!= null) {
                    mView.onKuwei(shelf);
                }
            }
        });
    }

    @Override
    protected boolean onRfid(String epc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mView!= null) {
                    mView.onBarcode(epc,epc);
                }
            }
        });
        return false;
    }

    @Override
    protected void changeReadingStatus(boolean b) {
        super.changeReadingStatus(b);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mView != null) {
                    mView.onScanStatus(b);
                }
            }
        });
    }
}
